//
//  CDExportViewController.swift
//  PDF
//
//  Created by Kellv on 2024/4/20.
//

import UIKit

class CDExportViewController: CDBaseAllViewController {

    var images:[UIImage] = []
    lazy var moreActionAlert: CDSheetAlert = {
        let alert = CDSheetAlert(items: [.exportPdf,.exportImages,.exportLongPicture])
        alert.actionHandler = {[weak self] item in
            guard let self = self else {
                return
            }
            self.moreAction(action: item)
        }
        return alert
    }()
    
    lazy var congratulationAlert: CDThankAlert = {
        let tmpV = CDThankAlert()
        tmpV.load(title: "Congratulations".localize(), subTitle: "Your tutorial is over".localize(), iconImage: "popup_congratulations")
        tmpV.actionHandler = { [weak self] in
            guard let self = self else {
                return
            }
            self.goHome()
        }
        return tmpV
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let cancel = UIButton(type: .app_back)
        cancel.addTarget(self, action: #selector(onCancelHandler), for: .touchUpInside)
        cancel.frame = CGRect(x: 16, y: 16, width: 32, height: 32)
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: cancel)
        
        let export = UIButton(type: .export)
        export.addTarget(self, action: #selector(onExportHanlder), for: .touchUpInside)
        export.size = CGSize(width: 80, height: 32)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: export)
        
        let scroller = UIScrollView(frame: CGRect(x: 0, y: 0, width: CDSCREEN_WIDTH, height: CDViewHeight))
        scroller.backgroundColor = .white
        view.addSubview(scroller)
        for i in 0..<images.count {
            let image = images[i]
            let imageView = UIImageView(frame: CGRect(x: 0, y: 240.0 * CGFloat(i), width: CDSCREEN_WIDTH, height: 240.0))
            imageView.image = image
            scroller.addSubview(imageView)
        }
        scroller.contentSize = CGSize(width: CDSCREEN_WIDTH, height: 240.0 * CGFloat(images.count))
    }

   @objc func onCancelHandler() {
       self.dismiss(animated: true)
    }
    
    @objc func onExportHanlder() {
        self.moreActionAlert.show()

    }
    
    func moreAction(action: SheetItem) {
        switch action {
        case .exportPdf:
            CDHUDManager.shared.showWait()
            var pdfPath = String.RootPath().appendingPathComponent(str: "\(GetTimestamp()).pdf")
            CDCovertTools.imageToPdf(images: images, pdfPath: &pdfPath)
            CDSignalTon.shared.saveFileWithUrl(fileUrl: pdfPath.pathUrl, folderInfo: nil)
            CDHUDManager.shared.hideWait()
            self.congratulationAlert.show()
            NotificationCenter.default.post(name: NSNotification.Name("addMediaSuccess"), object: nil)
            
        case .exportImages:
            CDHUDManager.shared.showWait()

            DispatchQueue.global().async { [self] in
                images.forEach { image in
                    CDSignalTon.shared.saveOrigialImage(image, nil)
                    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
                }
                DispatchQueue.main.async {        
                    CDHUDManager.shared.hideWait()

                    self.congratulationAlert.show()
                    NotificationCenter.default.post(name: NSNotification.Name("addMediaSuccess"), object: nil)
                }
            }
        case .exportLongPicture:
            CDHUDManager.shared.showWait()
            var longImage = UIImage()
            CDCovertTools.covertToLongImage(images: images, longImage: &longImage)
            CDSignalTon.shared.saveOrigialImage(longImage, nil)
            UIImageWriteToSavedPhotosAlbum(longImage, nil, nil, nil)
            CDHUDManager.shared.hideWait()

            self.congratulationAlert.show()
            NotificationCenter.default.post(name: NSNotification.Name("addMediaSuccess"), object: nil)

        default:
            break
        }
        
    }
    
    func goHome() {
        CDSignalTon.shared.tab = CDTabBarViewController()
        let myDelegate = UIApplication.shared.delegate as! CDAppDelegate
        myDelegate.window?.rootViewController = CDSignalTon.shared.tab
    }
}
